package com.sxau.software_educational_system_backend.controller;


import com.aliyun.oss.model.OSSObjectSummary;
import com.sxau.software_educational_system_backend.annotation.AdminOneOrTwo;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyOne;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyTwo;
import com.sxau.software_educational_system_backend.common.BaseResponse;
import com.sxau.software_educational_system_backend.common.ErrorCode;
import com.sxau.software_educational_system_backend.common.ResultUtils;
import com.sxau.software_educational_system_backend.domain.entity.File;
import com.sxau.software_educational_system_backend.domain.req.ReqSaveFileDTO;
import com.sxau.software_educational_system_backend.domain.res.OssCredentialVO;
import com.sxau.software_educational_system_backend.domain.res.ResFileVO;
import com.sxau.software_educational_system_backend.service.FileService;
import com.sxau.software_educational_system_backend.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/oss")
@Api(tags = "文件接口")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AliOssUtil aliOssUtil;

//    /**
//     * 文件上传
//     * @param file
//     * @return
//     */
//    @PostMapping("/yz/upload")
//    @ApiOperation("文件上传")
//    @AdminOneOrTwo
//    public BaseResponse<String> upload(MultipartFile file){
//        log.info("文件上传：{}",file);
//        if (file == null) {
//            return ResultUtils.error(ErrorCode.FILE_NOT_FOUND);
//        }
//
//        try {
//            //原始文件名
//            String originalFilename = file.getOriginalFilename();
//            //截取原始文件名的后缀
//            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//            //构造新文件名称
//            String objectName = UUID.randomUUID().toString() + extension;
//
//            //文件的请求路径
//            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
//            File fileObj = new File();
//            fileObj.setFileName(objectName);
//            fileObj.setUrl(filePath);
//            fileObj.setSize(file.getSize() + "");
//
//            fileService.save(fileObj);
//            return ResultUtils.success(filePath);
//        } catch (IOException e) {
//        log.error("文件上传失败：{}", e);
//        }
//
//        return ResultUtils.error(ErrorCode.UPLOAD_FAILED);
//    }

    /**
     * 存储文件
     * @param dto
     * @return
     */
    @PostMapping("/yz")
    @ApiOperation("存储文件")
    public BaseResponse<String> saveFile(@RequestBody @Validated ReqSaveFileDTO dto){
        boolean result = fileService.saveFile(dto);
        if (!result){
            return ResultUtils.error(ErrorCode.SAVE_FAILED);
        }
        return ResultUtils.success();
    }

    @GetMapping("/file")
    @ApiOperation("OSS文件列表")
    public BaseResponse<List<OSSObjectSummary>> listOssFiles(){
        List<OSSObjectSummary> fileList = fileService.listOssFiles();
        return ResultUtils.success(fileList);
    }

    @DeleteMapping("/yz/{id}")
    @ApiOperation("根据id删除文件")
    @AdminOneOrTwo
    public BaseResponse<String> deleteFile(@PathVariable Long id){
        boolean result = fileService.deleteFile(id);
        if (!result){
            return ResultUtils.error(ErrorCode.DELETE_FAILED);
        }
        return ResultUtils.success();
    }

    @GetMapping("/data/files")
    @ApiOperation("文件列表")
    public BaseResponse<List<ResFileVO>> listFiles(){
        List<ResFileVO> files = fileService.listFiles();
        return ResultUtils.success(files);
    }



    @GetMapping("/yz")
    @ApiOperation("获取OSS临时凭证")
    @AdminOneOrTwo
    public BaseResponse<OssCredentialVO> getOssCredential() {
        OssCredentialVO credential = aliOssUtil.getOssTemporaryCredential();
        return ResultUtils.success(credential);
    }

}
